﻿using Microsoft.Win32;
using Neonode.Forms;
using System;

namespace StopWatch
{
    class FrmSettings : ListView
    {
        public FrmSettings()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            string str1, str2, str3;
            RegistryKey key;

            Title.Text = "Settings";

            key = Registry.LocalMachine.OpenSubKey(@"Software\Herbie\StopWatch\");

            if (((int)key.GetValue("KeepAwake")) == 1)
            {
                str1 = "Keep Awake: Yes";
            }
            else
            {
                str1 = "Keep Awake: No";
            }

            switch ((int)key.GetValue("LCDColor"))
            {
                case 0:
                    str2 = "LCD Color: White";
                    break;

                case 1:
                    str2 = "LCD Color: Red";
                    break;

                case 2:
                    str2 = "LCD Color: Green";
                    break;

                case 3:
                    str2 = "LCD Color: Blue";
                    break;

                default:
                    str2 = "LCD Color: White";
                    break;
            }

            if (((int)key.GetValue("FastReset")) == 1)
            {
                str3 = "Fast Reset: Enabled";
            }
            else
            {
                str3 = "Fast Reset: Disabled";
            }

            key.Close();

            ListViewItem listItem1 = new ListViewItem(str1, 0, "Keep awake while the stopwatch is counting");
            ListViewItem listItem2 = new ListViewItem(str2, 1, "Select LCD background color");
            ListViewItem listItem3 = new ListViewItem(str3, 2, "Enable/disable LeftSweep reset when stopwatch is counting");
            ImageList imgList = new ImageList();
            imgList.Images.Add(base.GetType().Assembly, "StopWatch.Properties.Resources.resources", "Clock");
            imgList.Images.Add(base.GetType().Assembly, "StopWatch.Properties.Resources.resources", "Color");
            imgList.Images.Add(base.GetType().Assembly, "StopWatch.Properties.Resources.resources", "Lightning");
            ImageList = imgList;
            Items.Add(listItem1);
            Items.Add(listItem2);
            Items.Add(listItem3);
            Click += new EventHandler(this.lstSettings_Click);
        }
        
         private void lstSettings_Click(object sender, EventArgs e)
        {
            if (Items[0].Selected)
            {
                RegistryKey key = Registry.LocalMachine.CreateSubKey(@"Software\Herbie\StopWatch\");
                if (Items[0].Text == "Keep Awake: Yes")
                {
                    Items[0].Text = "Keep Awake: No";
                    key.SetValue("KeepAwake", "0", RegistryValueKind.DWord);
                }
                else
                {
                    Items[0].Text = "Keep Awake: Yes";
                    key.SetValue("KeepAwake", "1", RegistryValueKind.DWord);
                }
                key.Close();
            }

            if (Items[1].Selected)
            {
                RegistryKey key = Registry.LocalMachine.CreateSubKey(@"Software\Herbie\StopWatch\");
                
                switch ((int)key.GetValue("LCDColor"))
                {
                    case 0:
                        Items[1].Text = "LCD Color: Red";
                        key.SetValue("LCDColor", "1", RegistryValueKind.DWord);
                        break;

                    case 1:
                        Items[1].Text = "LCD Color: Green";
                        key.SetValue("LCDColor", "2", RegistryValueKind.DWord);
                        break;

                    case 2:
                        Items[1].Text = "LCD Color: Blue";
                        key.SetValue("LCDColor", "3", RegistryValueKind.DWord);
                        break;

                    case 3:
                        Items[1].Text = "LCD Color: White";
                        key.SetValue("LCDColor", "0", RegistryValueKind.DWord);
                        break;

                    default:
                        Items[1].Text = "LCD Color: White";
                        key.SetValue("LCDColor", "0", RegistryValueKind.DWord);
                        break;
                }
                key.Close();
            }

            if (Items[2].Selected)
            {
                RegistryKey key = Registry.LocalMachine.CreateSubKey(@"Software\Herbie\StopWatch\");
                if (Items[2].Text == "Fast Reset: Enabled")
                {
                    Items[2].Text = "Fast Reset: Disabled";
                    key.SetValue("FastReset", "0", RegistryValueKind.DWord);
                }
                else
                {
                    Items[2].Text = "Fast Reset: Enabled";
                    key.SetValue("FastReset", "1", RegistryValueKind.DWord);
                }
                key.Close();
            }
        }
    }
}
